import Vue from 'vue';
let scroll = {};
scroll.config = {};
scroll.install = (Vue) => {
  Vue.directive('scroll', {
    bind(el, binding) {
      let atBottom = false;
      let dataIsEmpty = false;
      el.addEventListener('scroll', () => {
        const virtualFunc = binding.value;
        //  计算数据包裹框是否触底
        if (el.scrollTop + el.clientHeight === el.scrollHeight) {
          //  防止多次触底发起请求  
          if (!atBottom) {
            atBottom = true
            //  防止后台数据已经为空的时候还在发起请求
            if (!dataIsEmpty) {
              virtualFunc().then((res) => {
                atBottom = false
                dataIsEmpty = !Boolean(res)
              })
            }
          }
        }
      });
    }
  })
}
Vue.use(scroll);
